package utils;

import uci.rt.ccr1c.engine.matrix.Mat;
import uci.rt.ccr1c.engine.reader.GenericFileReader;

public class Grid100Reader extends GenericFileReader<Mat>
{
	protected int rows;
	protected int cols;
	public Grid100Reader(String filePath, int rows, int cols)
	{
		super(filePath);
		this.rows=rows;
		this.cols=cols;
	}

	@Override
	public Mat[] get()
	{
		return list.toArray(new Mat[list.size()]);
	}

	@Override
	protected Mat newInstance(String row)
	{
		String[] parts = row.split(" |,|;");
		if(parts.length!=(rows*cols))
		{
			try
			{
				throw new Exception("Grid File inconsistent no "+rows+" x "+cols+" values ["+parts.length+"]");
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		double[][] res = new double[rows][cols];
		for (int r = 0; r < rows; r++)
		{
			for (int c = 0; c < cols; c++)
			{
				res[r][c]=Double.parseDouble(parts[r*rows+c]);
			}

		}

		return new Mat("Grid ", res);
	}

}
